/*
 * Decompiled with CFR 0.150.
 *
 * Could not load the following classes:
 *  io.idc.core.log.publisher.MagicLogPublisher
 *  io.idc.core.tool.utils.Charsets
 *  org.apache.velocity.Template
 *  org.apache.velocity.VelocityContext
 *  org.apache.velocity.app.VelocityEngine
 *  org.apache.velocity.context.Context
 *  org.springframework.beans.factory.annotation.Qualifier
 *  org.springframework.stereotype.Component
 */
package com.chenfan.magic.resolver;

import com.chenfan.magic.core.IdcEngineEntry;
import com.chenfan.magic.core.MagicLogPublisher;
import com.chenfan.magic.entity.TTLDetailModel;
import com.chenfan.magic.utils.Charsets;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
/**
 * @author lm
 */
@Component
public class VelocityResolver implements __IResolver {
    public static final String TYPE_CODE = "velocity";
    @Qualifier(value = "velocityResolverEngine")
    private VelocityEngine velocityResolverEngine;

    @Override
    public String resolve(Map<String, Object> idcRuntimePool, String templateContent, Map<String, Object> paramMap) {
        TTLDetailModel ttlDetailModel = new TTLDetailModel();
        ttlDetailModel.start();
        String result = "";
        try {
            Template template = this.velocityResolverEngine.getTemplate(templateContent, Charsets.UTF_8_NAME);
            VelocityContext context = new VelocityContext(paramMap);
            StringWriter writer = new StringWriter();
            template.merge((Context) context, (Writer) writer);
            result = writer.toString();
            MagicLogPublisher.publishResolverEvent((String) TYPE_CODE, (String) templateContent, (String) result, paramMap, (String) (ttlDetailModel.end().getTtl() + ""));
        } catch (Exception e) {
            e.printStackTrace();
            MagicLogPublisher.publishResolverEvent((String) TYPE_CODE, (String) templateContent, (String) result, paramMap, (String) (ttlDetailModel.end().getTtl() + ""), (Throwable) e);
            IdcEngineEntry.throwException(idcRuntimePool, null, e);
        }
        return result;
    }

    public VelocityResolver(VelocityEngine velocityResolverEngine) {
        this.velocityResolverEngine = velocityResolverEngine;
    }
}

